<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客正文</title>
    <link rel="stylesheet" href="css/conmmon.css">
    <link rel="stylesheet" href="css/blog_content.css">
    <link rel="stylesheet" href="editor.md/css/editormd.preview.min.css" />
    <script src="js/jquery.min.js"></script>
    <script src="editor.md/editormd.js"></script>
    <script src="editor.md/lib/marked.min.js"></script>
    <script src="editor.md/lib/prettify.min.js"></script>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="https://p.qqan.com/up/2020-5/2020052808383986122.jpg" alt="">
        <span class="title">我的博客系统</span>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
        <a href="index.html">主页</a>
        <a id="isedit" href="blog_add.html">写博客</a>
        <a id="islogin" href="login.html">登陆</a>
    </div>
    <!-- 版心 -->
    <div class="container">
        <!-- 左侧个人信息 -->
        <div class="container-left">
            <div class="card">
                <img id="pic"  class="avtar" alt="">
                <h3 id="username"></h3>
                <a  href="https://gitee.com/login"  target="_blank">gitee 地址</a>
                <div class="counter">
                    <span>文章</span>
                    <span>访问数</span>
                </div>
                <div class="counter">
                    <span id="acount"></span>
                    <span id="readcount"></span>
                </div>
            </div>
        </div>
        <!-- 右侧内容详情 -->
        <div class="container-right">
            <div class="blog-content">
                <!-- 博客标题 -->
                <h3 id="title"></h3>
                <!-- 博客时间 -->
                <div class="date"><span id="ctime" ></span></div>

                <!-- 博客正文 -->
                <div id="context">
            
                </div>
               
            </div>
        </div>
    </div>
    <script>

         //获取当前url中某个参数的方法
         function getURLParam(key){
            var params=location.search;
            if(params.indexOf("?")>=0){
                params=params.substring(1).split("&");
                var paramArr=params;
                for(var i=0;i<paramArr.length;i++){
                    var namesvalues=paramArr[i].split("=");
                    if(namesvalues[0]==key){
                        return namesvalues[1];
                    }
                }
            }else{
                return "";
            }
        }


        //加载文章
         //1.先得到url中的文章id
         var aid=getURLParam("id");
         if(aid==null || aid<=0 || aid==""){
            alert("抱歉：访问失败，非法参数！");
            //todo： 跳转到首页
         }else{
            function loadArticle(){
            //2.请求后端得到文章详情并进行显示
            jQuery.ajax({
                url:"artdetail",
                type:"POST",
                data:{"aid":aid},
                success:function(result){
                    if(result!=null && result.succ==200){
                        if(result.data!=null && result.data.aid>0){
                            //成功查询详情
                            jQuery("#title").text(result.data.title);
                            jQuery("#ctime").text(result.data.createtime);
                            jQuery("#readcount").text(result.data.readcount);
                            // jQuery("#context").html(result.data.context);
                            markdownToHtml(result.data.context);
                        }else{
                            alert("抱歉！当前文章不存在！")
                        }
                    }else{
                        alert("抱歉！后端程序不可用！")
                    }

                }
            });     
        }
        
        loadArticle();

        //加载用户的个人信息
        function loadUserInfo(){
             //1.先得到url中的文章id
         var aid=getURLParam("id");
         //请求后端查询用户的详情信息
         jQuery.ajax({
             url:"getuserbyaid",
             type:"GET",
             data:{"aid":aid},
             success:function(result){
                 if(result!=null && result.succ==200){
                     if(result.data!=null && result.data.uid>0){
                        jQuery("#username").text(result.data.username);
                         jQuery("#acount").text(result.data.acount);
                         jQuery("#pic").attr("src","https://tse1-mm.cn.bing.net/th/id/R-C.7c4a1b3b6990ac721cca9e7ae03a2529?rik=ihC5JDh0qtafug&riu=http%3a%2f%2fwww.pp3.cn%2fuploads%2f20120508BZ-YB%2f7q.jpg&ehk=7F3HCtI%2byCmJSYu6rYlezLLAcBRd0NttlML7gLSpMKg%3d&risl=&pid=ImgRaw&r=0");
                   
                     }else{
                         alert("参数错误"+result.msg);
                     }
                 }else{
                     alert("后端程序不可用");
                 }
             }

         });
        }
        loadUserInfo();

        //当前文章阅读量+1
        function addRcount(){
            jQuery.ajax({
                url:"addrcount",
                type:"POST",
                data:{"aid":aid},
                success:function(result){
                    if(result!=null && result.succ==200){
                       console.log(result.data);
                    }else{
                        alert("后端程序不可用 ");
                    }
                }
            });
        }
        addRcount();

        //判断是否登录状态
        function isLogin(){
            jQuery.ajax({
                url:"loginUserInfo",
                type:"GET",
                success:function(result){
                    if(result!=null && result.succ==200 && 
                    result.data!=null && result.data.uid>0){
                        //登录状态
                        jQuery("#isedit").attr("href","blog_edit.html");
                        jQuery("#islogin").text("退出");
                        jQuery("#islogin").attr("href","javascript: logout()");
                    }else{
                        //非登录状态
                        jQuery("#isedit").attr("href","login.html");
                        jQuery("#islogin").text("登录");
                        jQuery("#islogin").attr("href","login.html");
                }
            }
        });
     } 
        isLogin();

                       //退出事件
                        function logout(){
                            if(confirm("是否退出？")){
                                //1.session清空
                                jQuery.ajax({
                                    url:"loginout",
                                    type:"POST",
                                    success:function(result){
                                        if(result!=null && result.succ==200 && result.data==1){
                                            alert("退出成功！");
                                            //当前页面刷新
                                            location.href=location.href;
                                        }else{
                                            alert("抱歉，操作出错，请重试！");
                                        }
                                    }
                                });

                            }
                        }
    }

    function markdownToHtml(md){
        editormd.markdownToHTML("context", {
            markdown : md, // Also, you can dynamic set Markdown text
            //     // htmlDecode : true,  // Enable / disable HTML tag encode.
            //     // htmlDecode : "style,script,iframe",  // Note: If enabled, you should filter some dangerous HTML tags for website security.
            });
    }
    </script>

</body>

</html>